package  
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.FileReference;
	import org.flixel.FlxG;
	/**
	 * ...
	 * @author morgan
	 */
	public class PlayerRecording
	{
		public static const EVT_LEFT_DOWN:uint = 0;
		public static const EVT_LEFT_UP:uint = 1;
		public static const EVT_RIGHT_DOWN:uint = 2;
		public static const EVT_RIGHT_UP:uint = 3;
		public static const EVT_JUMP_DOWN:uint = 4;
		public static const EVT_JUMP_UP:uint = 5;
		public static const EVT_THROW_DOWN:uint = 6;
		public static const EVT_THROW_UP:uint = 7;
		
		public var record:Array = [];
		public function PlayerRecording() 
		{
			
		}
		
		public function addInput(event:uint, time:Number):void
		{
			record.push([time, event]);
		}
		
		public function getUntil(time:Number):Array
		{
			var ret:Array = []
			while(record.length > 0 && record[0][0] <= time)
			{
				ret.push(record.shift()[1]);
			}
			return ret;
		}
		
		public function save():void
		{
			var file:FileReference = new FileReference();
			var recStr:String = "";
			for (var i:int = 0; i < record.length; i++)
			{
				recStr += record[i][0].toString() + "," + record[i][1].toString() + "\n";
			}
			file.save(recStr, ".rec");
		}
		
		public function load():void
		{
			var file:FileReference = new FileReference();
			file.browse();
			file.addEventListener(Event.SELECT, function(evt:Event):void { 
				file.addEventListener(Event.COMPLETE, function(evt2:Event):void {
					loadRecordString(file.data.toString());
				});
				file.load();
			});
		}
		
		public function loadRecordString(rec:String):void
		{
			record = [];
			var lines:Array = rec.split("\n");
			for each (var line:String in lines)
			{
				var parts:Array = line.split(",");
				addInput(parseInt(parts[1]), parseFloat(parts[0]));
			}
		}
	}

}